package com.huafv.flyauth.security.handle;

import com.alibaba.fastjson2.JSON;
import com.huafv.flyauth.core.domain.Result;
import com.huafv.flyauth.utils.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)  {
        String message = "";
        int code = 0;
        if(request.getRequestURI().equals("/user/login")){
            code = 401;
            message = "用户名或密码错误";
        }else{
            code = 413;
            message = "请先登录";
        }

        Result result = Result.error(code,message);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);
    }
}
